module ApplicationHelper
  def odevity_tag index
    (index + 1) % 2 == 0 ? 'even' : 'odd'
  end

  def empty_record_tag
    "<tr><td colspan=\"10\" class=\"empty\">没有任何记录！</td></tr>"
  end

  def crud_for_list_tag record, options = {}
    crud_actions = []
    crud_actions << "#{image_tag 'ico_show.png', :alt => '查看'} #{link_to '查看', (options[:show_path] || record)}" unless options[:discard_show]
    crud_actions << "#{image_tag 'ico_edit.png', :alt => '编辑'} #{link_to '编辑', (options[:edit_path] || url_for(record) + '/edit')}" unless options[:discard_edit]
    crud_actions << "#{image_tag 'ico_destroy.png', :alt => '删除'} #{link_to '删除', (options[:destroy_path] || record), :confirm => '确定删除该记录？', :method => :delete}" if !options[:discard_destroy]
    crud_actions.join ' | '
  end

  def crud_for_show_tag record, options = {}
    crud_actions = []
    crud_actions << "#{link_to '添加', url_for(record.class.new) + '/new', :class => 'button'}" unless options[:discard_new]
    crud_actions << "#{link_to '编辑', (options[:edit_path] || url_for(record) + '/edit'), :class => 'button'}" unless options[:discard_edit]
    crud_actions << "#{link_to '删除', (options[:destroy_path] || record), :class => 'button', :confirm => '确定删除该记录？', :method => :delete}" if !options[:discard_destroy]
    crud_actions << "#{link_to '返回', (options[:return_path] || record.class.new), :class => 'button'}" unless options[:discard_return]
    crud_actions.join ' '
  end

  def signined?
    session[:user_id]
  end

  def accessible_to roles = []
    roles.include? session[:role]
  end

  def human_boolean_for boolean
    boolean ? '是' : '否'
  end

  def human_date_for date
    date.strftime '%Y-%m-%d' unless date.blank?
  end

  def human_datetime_for datetime
    datetime.strftime '%Y-%m-%d %H:%M' unless datetime.blank?
  end

  def human_image_for image
    image.blank? ? '无' : image_tag(image.uri)
  end

  def human_text_for content
    RedCloth.new(content).to_html unless content.blank?
  end

  def flash_tag
    if flash[:error]
      content_tag 'p', h(flash[:error]), :class => 'notice error'
    elsif flash[:notice]
      content_tag 'p', h(flash[:notice]), :class => 'notice'
    end
  end
end
